#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ssb.h"


// write out ssb's data table in binary format
void datewrite(FILE * fp){
	struct ddate tmp;
	char data [32] = {0};
	char buf[512] = {0};
	int count = 0, i = 0,prev = 0;
	FILE * out[17];

	for(i=0;i<17;i++){
		char path[32] = {0};
		sprintf(path,"DATE%d",i);
		out[i] = fopen(path,"w");
	}

	while(fgets(buf,sizeof(buf),fp)!= NULL){
		for(i = 0, prev = 0,count=0; buf[i] !='\n';i++){
			memset(data,0,sizeof(data));
			if (buf[i] == '|'){
				strncpy(data,buf+prev,i-prev);
				prev = i+1;
				switch(count){
					case 0: tmp.datekey = strtol(data,NULL,10); 
						fwrite(&(tmp.datekey),sizeof(tmp.datekey),1,out[0]);
						break; 
					case 1: strcpy(tmp.date,data); 
						fwrite(&(tmp.date),sizeof(tmp.date),1,out[1]);
						break;
					case 2: strcpy(tmp.dayofweek,data);
						fwrite(&(tmp.dayofweek),sizeof(tmp.dayofweek),1,out[2]);
						break;
					case 3: strcpy(tmp.month,data);
						fwrite(&(tmp.month),sizeof(tmp.month),1,out[3]);
						break;
					case 4: tmp.year = strtol(data,NULL,10);
						fwrite(&(tmp.year),sizeof(tmp.year),1,out[4]);
						break;
					case 5: tmp.yearmonthnum = strtol(data,NULL,10); 
						fwrite(&(tmp.yearmonthnum),sizeof(tmp.yearmonthnum),1,out[5]);
						break;
					case 6: strcpy(tmp.yearmonth,data);
						fwrite(&(tmp.yearmonth),sizeof(tmp.yearmonth),1,out[6]);
						break;
					case 7: tmp.daynuminweek = strtol(data,NULL,10); 
						fwrite(&(tmp.daynuminweek),sizeof(tmp.daynuminweek),1,out[7]);
						break;
					case 8: tmp.daynuminmonth = strtol(data,NULL,10); 
						fwrite(&(tmp.daynuminmonth),sizeof(tmp.daynuminmonth),1,out[8]);
						break;
					case 9: tmp.daynuminyear =  strtol(data,NULL,10); 
						fwrite(&(tmp.daynuminyear),sizeof(tmp.daynuminyear),1,out[9]);
						break;
					case 10: tmp.monthnuminyear = strtol(data,NULL,10);
						fwrite(&(tmp.monthnuminyear),sizeof(tmp.monthnuminyear),1,out[10]);
						break;
					case 11: tmp.weeknuminyear = strtol(data,NULL,10);
						fwrite(&(tmp.weeknuminyear),sizeof(tmp.weeknuminyear),1,out[11]);
						break;
					case 12: strcpy(tmp.sellingseason,data);
						fwrite(&(tmp.sellingseason),sizeof(tmp.sellingseason),1,out[12]);
						break;
					case 13: tmp.lastdayinweekfl = data[0]; 
						fwrite(&(tmp.lastdayinweekfl),sizeof(char),1,out[13]);
						break;
					case 14: tmp.lastdayinmonthfl = data[0]; 
						fwrite(&(tmp.lastdayinmonthfl),sizeof(char),1,out[14]);
						break;
					case 15: tmp.holidayfl = data[0]; 
						fwrite(&(tmp.holidayfl),sizeof(char),1,out[15]);
						break;
				}
				count ++;
			}
		}
		tmp.weekdayfl = buf[i-1];
		fwrite(&(tmp.weekdayfl),sizeof(char),1,out[16]);
	}
	for(i=0;i<17;i++){
		fclose(out[i]);
	}
}

void lineorder(FILE *fp){
	struct lineorder tmp;
	char data[32] = {0};
	char buf[512] = {0};
	int count = 0, i = 0, prev = 0;
	FILE * out[17];

	for(i=0;i<17;i++){
		char path[32] = {0};
		sprintf(path,"LINEORDER%d",i);
		out[i] = fopen(path,"w");
	}


	while(fgets(buf,sizeof(buf),fp)!= NULL){
		for(i=0,prev=0,count=0;buf[i]!='\n';i++){
			memset(data,0,sizeof(data));
			if (buf[i] == '|'){
				strncpy(data,buf+prev,i-prev);
				prev = i+1;
				switch(count){
					case 0: tmp.orderkey = strtol(data,NULL,10);
						fwrite(&(tmp.orderkey),sizeof(tmp.orderkey),1,out[0]);
						break;
					case 1: tmp.linenumber = strtol(data,NULL,10);
						fwrite(&(tmp.linenumber),sizeof(tmp.linenumber),1,out[1]);
						break;
					case 2: tmp.custkey = strtol(data,NULL,10);
						fwrite(&(tmp.custkey),sizeof(tmp.custkey),1,out[2]);
						break;
					case 3: tmp.partkey = strtol(data,NULL,10);
						fwrite(&(tmp.partkey),sizeof(tmp.partkey),1,out[3]);
						break;
					case 4: tmp.suppkey = strtol(data,NULL,10);
						fwrite(&(tmp.suppkey),sizeof(tmp.suppkey),1,out[4]);
						break;
					case 5: tmp.orderdate = strtol(data,NULL,10);
						fwrite(&(tmp.orderdate),sizeof(tmp.orderdate),1,out[5]);
						break;
					case 6: strcpy(tmp.orderpriority,data);
						fwrite(&(tmp.orderpriority),sizeof(tmp.orderpriority),1,out[6]);
						break;
					case 7: tmp.shippriority = data[0];
						fwrite(&(tmp.shippriority),sizeof(tmp.shippriority),1,out[7]);
						break;
					case 8: tmp.quantity = strtol(data,NULL,10);
						fwrite(&(tmp.quantity),sizeof(tmp.quantity),1,out[8]);
						break;
					case 9: tmp.extendedprice = strtol(data,NULL,10);
						fwrite(&(tmp.extendedprice),sizeof(tmp.extendedprice),1,out[9]);
						break;
					case 10: tmp.ordtotalprice = strtol(data,NULL,10);
						fwrite(&(tmp.ordtotalprice),sizeof(tmp.ordtotalprice),1,out[10]);
						break;
					case 11: tmp.discount = strtol(data,NULL,10);
						fwrite(&(tmp.discount),sizeof(tmp.discount),1,out[11]);
						break;
					case 12: tmp.revenue = strtol(data,NULL,10);
						fwrite(&(tmp.revenue),sizeof(tmp.revenue),1,out[12]);
						break;
					case 13: tmp.supplycost = strtol(data,NULL,10);
						fwrite(&(tmp.supplycost),sizeof(tmp.supplycost),1,out[13]);
						break;
					case 14: tmp.tax = strtol(data,NULL,10);
						fwrite(&(tmp.tax),sizeof(tmp.tax),1,out[14]);
						break;
					case 15: tmp.commitdate = strtol(data,NULL,10);
						fwrite(&(tmp.commitdate),sizeof(tmp.commitdate),1,out[15]);
						break;
					default : break;
				}
				count ++;
			}
		}
		strncpy(tmp.shipmode,buf+prev,i-prev);
		fwrite(&(tmp.shipmode),sizeof(tmp.shipmode),1,out[16]);
	}
	for(i=0;i<17;i++){
		fclose(out[i]);
	}
}

void part(FILE *fp){
	struct part tmp;
	char data[32] = {0};
	char buf[512] = {0};
	int count = 0, i=0, prev = 0;
	FILE * out[17];

	for(i=0;i<9;i++){
		char path[32] = {0};
		sprintf(path,"PART%d",i);
		out[i] = fopen(path,"w");
	}


	while(fgets(buf,sizeof(buf),fp)!= NULL){
		for(i=0,prev=0,count=0;buf[i]!='\n';i++){
			memset(data,0,sizeof(data));
			if(buf[i] == '|'){
				strncpy(data,buf+prev,i-prev);
				prev = i+1;
				switch(count){
					case 0: tmp.partkey = strtol(data,NULL,10);
						fwrite(&(tmp.partkey),sizeof(tmp.partkey),1,out[0]);
						break;
					case 1: strcpy(tmp.name,data);
						fwrite(&(tmp.name),sizeof(tmp.name),1,out[1]);
						break;
					case 2: strcpy(tmp.mfgr,data);
						fwrite(&(tmp.mfgr),sizeof(tmp.mfgr),1,out[2]);
						break;
					case 3: strcpy(tmp.category,data);
						fwrite(&(tmp.category),sizeof(tmp.category),1,out[3]);
						break;
					case 4: strcpy(tmp.brand1,data);
						fwrite(&(tmp.brand1),sizeof(tmp.brand1),1,out[4]);
						break;
					case 5: strcpy(tmp.color,data); 
						fwrite(&(tmp.color),sizeof(tmp.color),1,out[5]);
						break;
					case 6: strcpy(tmp.type,data);
						fwrite(&(tmp.type),sizeof(tmp.type),1,out[6]);
						break;
					case 7: tmp.size = strtol(data,NULL,10);
						fwrite(&(tmp.size),sizeof(tmp.size),1,out[7]);
						break;
					default:break;
				}
				count ++;
			}
		}
		strncpy(tmp.container,buf+prev,i-prev);
		fwrite(&(tmp.container),sizeof(tmp.container),1,out[8]);
	}
	for(i=0;i<9;i++){
		fclose(out[i]);
	}
}

void supplier(FILE *fp){
	struct supplier tmp;
	char data[32] = {0};
	char buf[512] = {0};
	int count=0, i=0, prev = 0;
	FILE * out[7];

	for(i=0;i<7;i++){
		char path[32] = {0};
		sprintf(path,"SUPPLIER%d",i);
		out[i] = fopen(path,"w");
	}


	while(fgets(buf,sizeof(buf),fp)!= NULL){
		for(i=0,prev=0,count=0;buf[i]!='\n';i++){
			memset(data,0,sizeof(data));
			if(buf[i]=='|'){
				strncpy(data,buf+prev,i-prev);
				prev = i +1;
				switch(count){
					case 0: tmp.suppkey = strtol(data,NULL,10);
						fwrite(&(tmp.suppkey),sizeof(tmp.suppkey),1,out[0]);
						break;
					case 1: strcpy(tmp.name,data);
						fwrite(&(tmp.name),sizeof(tmp.name),1,out[1]);
						break;
					case 2: strcpy(tmp.address,data);
						fwrite(&(tmp.address),sizeof(tmp.address),1,out[2]);
						break;
					case 3: strcpy(tmp.city,data);
						fwrite(&(tmp.city),sizeof(tmp.city),1,out[3]);
						break;
					case 4: strcpy(tmp.nation,data);
						fwrite(&(tmp.nation),sizeof(tmp.nation),1,out[4]);
						break;
					case 5: strcpy(tmp.region,data);
						fwrite(&(tmp.region),sizeof(tmp.region),1,out[5]);
						break;
					default: break;
				}
				count ++;
			}
		}
		strncpy(tmp.phone,buf+prev,i-prev);
		fwrite(&(tmp.phone),sizeof(tmp.phone),1,out[6]);
	}
	for(i=0;i<7;i++){
		fclose(out[i]);
	}
}

void customer(FILE *fp){
	struct customer tmp;
	char data[32] = {0};
	char buf[512] = {0};
	int count =0,i=0,prev = 0;
	FILE * out[8];

	for(i=0;i<8;i++){
		char path[32] = {0};
		sprintf(path,"CUSTOMER%d",i);
		out[i] = fopen(path,"w");
	}


	while(fgets(buf,sizeof(buf),fp)!= NULL){
		for(i=0,prev=0,count=0;buf[i]!='\n';i++){
			memset(data,0,sizeof(data));
			if(buf[i] == '|'){
				strncpy(data,buf+prev,i-prev);
				prev = i+1;
				switch(count){
					case 0: tmp.custkey = strtol(data,NULL,10);
						fwrite(&(tmp.custkey),sizeof(tmp.custkey),1,out[0]);
						break;
					case 1: strcpy(tmp.name,data);
						fwrite(&(tmp.name),sizeof(tmp.name),1,out[1]);
						break;
					case 2: strcpy(tmp.address,data);
						fwrite(&(tmp.address),sizeof(tmp.address),1,out[2]);
						break;
					case 3: strcpy(tmp.city,data);
						fwrite(&(tmp.city),sizeof(tmp.city),1,out[3]);
						break;
					case 4: strcpy(tmp.nation,data);
						fwrite(&(tmp.nation),sizeof(tmp.nation),1,out[4]);
						break;
					case 5: strcpy(tmp.region,data);
						fwrite(&(tmp.region),sizeof(tmp.region),1,out[5]);
						break;
					case 6: strcpy(tmp.phone,data);
						fwrite(&(tmp.phone),sizeof(tmp.phone),1,out[6]);
						break;
					default: break;
				}
				count ++;
			}
		}
		strncpy(tmp.mktsegment,buf+prev,i-prev);
		fwrite(&(tmp.mktsegment),sizeof(tmp.mktsegment),1,out[7]);
	}
	for(i=0;i<8;i++){
		fclose(out[i]);
	}
}


int main(int argc, char ** argv){
	FILE * in = NULL, *out = NULL;

	if (argc !=6){
		printf("Usage error: date lo part customer supplier\n");
		exit(-1);
	}

	in = fopen(argv[1],"r");
	if (!in){
		printf("cannot open file\n");
		exit(-1);
	}

	datewrite(in);
	fclose(in);

	in = fopen(argv[2],"r");
	lineorder(in);
	fclose(in);

	in = fopen(argv[3],"r");
	part(in);
	fclose(in);

	in = fopen(argv[4],"r");
	customer(in);
	fclose(in);

	in = fopen(argv[5],"r");
	supplier(in);
	fclose(in);
	return 0;
}
